# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re

from .raw_and_code_block import RawAndCodeBlock

class CodeBlock(RawAndCodeBlock):
    """CodeBlock parser"""
    def __init__(self, content):
        super(CodeBlock, self).__init__(content, '`', 'code_block')

    def _toHtml(self, content):
        content = content.strip()
        mat = re.match(r'(^\s*\+?\s*\b[a-zA-Z]+\b)', content)
        _class = 'rmk-code-block'
        _lang = ''
        _flag = '+'
        if mat and len(mat.groups()) > 0:
            _lang = mat.group(0)
            if _lang.startswith('+'):
                _class += ' collapse'
                _flag = '-'
                _lang = _lang[1:].strip()
            _class += ' lang-' + _lang
            content = re.sub(r'^[^\n\r]+(\r|\n)', '', content)
        _info = '<div class="code-info"><i>' + _flag + '</i><label>' + _lang + '</label></div>'
        return '<pre class="'+ _class +'">' + _info + '<code>' + content + '</code></pre>'